package algotithm.leetcode.array.test724;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/28-15:16
 * @Title 724. 寻找数组的中心索引
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1, 7, 3, 6, 5, 6};
        int index = solution.pivotIndex(nums);
        System.out.println(index);
    }

    public int pivotIndex(int[] nums) {
        int left = 0;
        int right = 0;
        int sum = 0;
        for (int i = 0;i < nums.length; i++) {
            sum += nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            left += nums[i];
            right = sum + nums[i] - left;
            if (left == right) {
                return i;
            }
        }
        return -1;
    }

}
